﻿using B.S.ProductionProject.Domain;
using B.S.ProductionProject.ErrorCode;
using B.S.ProductionProject.Read.WebAPI.Applications.Commands.ProductionPlanningCommand;
using MediatR;
using SqlSugar;

namespace B.S.ProductionProject.Read.WebAPI.Applications.CommandHandlers.ProductionPlanningCommandHandler
{
    public class GetProductionPlanningCommandHandler : IRequestHandler<GetProductionPlanningCommand, APIResult<APIPaging<ProductionPlanning>>>
    {
        private readonly ISqlSugarClient db;
        private readonly IConfiguration configuration;

        public GetProductionPlanningCommandHandler(ISqlSugarClient db, IConfiguration configuration)
        {
            this.db = db;
            this.configuration = configuration;
        }

        public async Task<APIResult<APIPaging<ProductionPlanning>>> Handle(GetProductionPlanningCommand request, CancellationToken cancellationToken)
        {
            APIResult<APIPaging<ProductionPlanning>> result = new APIResult<APIPaging<ProductionPlanning>>();
            result.code = APIResultCode.success;
            result.msg = "查询显示成功";

            var list = db.Queryable<ProductionPlanning>().Where(p => p.isDelete == false);

            if (!string.IsNullOrEmpty(request.AssociatedItemNumber))
            {
                list = list.Where(p => p.AssociatedItemNumber == request.AssociatedItemNumber);
            }
            if (request.AutomaticOrderGeneration != null)
            {
                list = list.Where(p => p.AutomaticOrderGeneration == request.AutomaticOrderGeneration);
            }
            if (!string.IsNullOrEmpty(request.DocumentNo))
            {
                list = list.Where(p => p.DocumentNo == request.DocumentNo);
            }
            if (!string.IsNullOrEmpty(request.DocumentMaker))
            {
                list = list.Where(p => p.DocumentMaker.Contains(request.DocumentMaker));
            }
            if (request.DocumentationFriDate != null)
            {
                list = list.Where(p => p.DocumentationDate >= request.DocumentationFriDate);
            }
            if (request.DocumentationEndDate != null)
            {
                list = list.Where(p => p.DocumentationDate <= request.DocumentationEndDate);
            }
            if (!string.IsNullOrEmpty(request.DocumentSubject))
            {
                list = list.Where(p => p.DocumentSubject.Contains(request.DocumentSubject));
            }

            int TotalCount = list.Count();
            int TotalPage = (int)Math.Ceiling(TotalCount * 1.0 / request.PageSize);
            list = list.OrderByDescending(p => p.ProductionPlanningId).Skip((request.PageIndex - 1) * request.PageSize).Take(request.PageSize);

            APIPaging<ProductionPlanning> paging = new APIPaging<ProductionPlanning>();
            paging.TotalCount = TotalCount;
            paging.TotalPage = TotalPage;
            paging.list = list.ToList();

            result.data = paging;

            return await Task.FromResult(result);
        }
    }
}
